class Node:
<<<<<<< HEAD
    def __init__(self, data=None):
        self.data = data
        self.next = None


class LinkedList:
    def __init__(self):
        self.head = Node()
        self.length = 0
=======
    def __init__(self, data=None, next=None):
        self.data = data
        self.next = next


class LinkedList:
    def __init__(self, head=Node(), length=0):
        self.head = head
        self.length = length
>>>>>>> cb519a86c6262d54fb5f195b611f552ad9552736

    def findposition(self, pos):
        """
        找到给定位置的上一个节点
        :param pos:
        :return:
        """
        tempPtr = self.head
        while pos > 0:
            tempPtr = tempPtr.next
            pos -= 1
        return tempPtr

    def finddata(self,data):
        """
        找到输入数据的位置
        :param data:
        :return:
        """
        pos = self. length - 1
        tempPtr = self.head
        while pos > 0:
            if data != tempPtr.data:
                tempPtr = tempPtr.next
                pos -= 1
            else:
                return pos

    def insertEle(self, data, pos):
        """
        在pos位置插入一个data
        :param data:
        :param pos:
        :return:
        """
        assert 0 <= pos <= self.length
        # tempPtr = self.head
        # while pos > 0:
        #     tempPtr = tempPtr.next
        #     pos -= 1
        tempPtr = self.findposition(pos)

        newNode = Node()
        newNode.data = data
        # newNode.next = tempPtr.next
        # tempPtr.next = newNode
        newNode.next, tempPtr.next = tempPtr.next, newNode
        self.length += 1

    def appendEle(self, data):
        """
        在末尾追加一个data
        :param data:
        :return:
        """
        self.insertEle(data, self.length)

    def popEle(self, pos):
        """
        按照位置pos删除元素
        :param pos:
        :return:
        """
        assert 0 <= pos <= self.length - 1
        tempPtr = self.findposition(pos)

        # if tempPtr.next is not None:
        delNode = tempPtr.next
        tempPtr.next = delNode.next
        self.length -= 1

    def removeEle(self, data):
        """
        按照数据data删除元素
        :param data:
        :return:
        """
        assert self.length > 0
        pos = self.finddata(data)
        self.popEle(pos)

    def amendEle(self,data, pos):
        """
        将pos位置的数据修改为data
        :param data:
        :param pos:
        :return:
        """
        tempPtr = self.findposition(pos)
        amendNode = tempPtr.next
        amendNode.data = data

    def forEle(self):
        assert self.length > 0
        tempPtr = self.head.next
        while tempPtr is not None:
                yield tempPtr.data
                tempPtr = tempPtr.next

    def __iter__(self):
        return self.forEle()

<<<<<<< HEAD
    def __len__(self):
        return self.length
=======

>>>>>>> cb519a86c6262d54fb5f195b611f552ad9552736


if __name__ == "__main__":
    l = LinkedList()
    l.appendEle(1)
    l.appendEle(22)
    l.insertEle(99,1)
    l.appendEle(55)
    # l.popEle(0)
    # l.removeEle(99)
    l.amendEle(88,1)

    for v in l:
        print(v)




